package com.nbcio.modules.erp.uom.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nbcio.common.core.domain.R;
import com.nbcio.modules.erp.uom.domain.ErpUom;
import com.nbcio.modules.erp.uom.domain.ErpUomConv;
import com.nbcio.modules.erp.uom.mapper.ErpUomMapper;
import com.nbcio.modules.erp.uom.mapper.ErpUomConvMapper;
import com.nbcio.modules.erp.material.domain.ErpMaterial;
import com.nbcio.modules.erp.material.mapper.ErpMaterialMapper;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@Tag(name = "计量单位")
@RestController
@RequestMapping("/erp/uom")
@RequiredArgsConstructor
public class ErpUomController {

    private final ErpUomMapper uomMapper;
    private final ErpUomConvMapper convMapper;
    private final ErpMaterialMapper materialMapper;

    @GetMapping("/page")
    public R<IPage<ErpUom>> page(@RequestParam int pageNum,
                                 @RequestParam int pageSize,
                                 @RequestParam(required = false) String uomCode,
                                 @RequestParam(required = false) String uomName) {
        LambdaQueryWrapper<ErpUom> qw = new LambdaQueryWrapper<>();
        if (uomCode != null && !uomCode.isEmpty()) qw.like(ErpUom::getUomCode, uomCode);
        if (uomName != null && !uomName.isEmpty()) qw.like(ErpUom::getUomName, uomName);
        return R.ok(uomMapper.selectPage(Page.of(pageNum, pageSize), qw));
    }
}

@Tag(name = "计量单位换算")
@RestController
@RequestMapping("/erp/uom")
@RequiredArgsConstructor
class ErpUomConvQueryController {

    private final ErpUomConvMapper convMapper;
    private final ErpUomMapper uomMapper;
    private final ErpMaterialMapper materialMapper;

    public static class UomOptionDTO {
        public String uomId;
        public String uomCode;
        public String uomName;
        public String rate; // 字符串返回，前端不做精度计算
        public boolean base;
    }

    @GetMapping("/conv")
    public R<java.util.List<UomOptionDTO>> conv(@RequestParam String materialId) {
        Long mid = Long.parseLong(materialId);
        ErpMaterial m = materialMapper.selectById(mid);
        java.util.List<ErpUomConv> convs = convMapper.selectList(new LambdaQueryWrapper<ErpUomConv>().eq(ErpUomConv::getMaterialId, mid));
        java.util.Set<Long> uomIds = new java.util.HashSet<>();
        if (m != null && m.getUomId() != null) uomIds.add(m.getUomId());
        for (ErpUomConv c : convs) { uomIds.add(c.getFromUomId()); uomIds.add(c.getToUomId()); }
        java.util.List<ErpUom> uoms = uomMapper.selectBatchIds(uomIds);
        java.util.Map<Long, ErpUom> id2uom = new java.util.HashMap<>();
        for (ErpUom u : uoms) id2uom.put(u.getUomId(), u);
        java.util.List<UomOptionDTO> out = new java.util.ArrayList<>();
        if (m != null && m.getUomId() != null) {
            ErpUom base = id2uom.get(m.getUomId());
            if (base != null) {
                UomOptionDTO dto = new UomOptionDTO();
                dto.uomId = String.valueOf(base.getUomId());
                dto.uomCode = base.getUomCode();
                dto.uomName = base.getUomName();
                dto.rate = "1";
                dto.base = true;
                out.add(dto);
            }
        }
        for (ErpUomConv c : convs) {
            ErpUom to = id2uom.get(c.getToUomId());
            if (to == null) continue;
            UomOptionDTO dto = new UomOptionDTO();
            dto.uomId = String.valueOf(to.getUomId());
            dto.uomCode = to.getUomCode();
            dto.uomName = to.getUomName();
            dto.rate = c.getRate() == null ? null : c.getRate().toPlainString();
            dto.base = (m != null && to.getUomId().equals(m.getUomId()));
            if (!dto.base) out.add(dto);
        }
        return R.ok(out);
    }
}


